<template>
  <j-modal title="填写取消信息" :width="600" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 18 }">
      <a-form-model-item label="取消原因" prop="cancelReason">
        <a-select v-model="form.cancelReason" placeholder="请选择">
          <a-select-option value="1">填写有误</a-select-option>
          <a-select-option value="2">对方拒检</a-select-option>
          <a-select-option value="3">个人原因</a-select-option>
          <a-select-option value="4">其他</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="拒检次数" prop="refuseNum">
        <a-input v-model="form.refuseNum" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="原因说明" prop="cancelReasonExplain">
        <a-textarea v-model="form.cancelReasonExplain" placeholder="请输入" :auto-size="{ minRows: 4, maxRows: 6 }" />
      </a-form-model-item>
      <a-form-model-item label="处置文档" class="item2">
        <p v-if="isOver">社区戒毒社区康复人员违法/违规行为报告.pdf</p>
        <p v-else>社区戒毒社区康复人员违规行为告诫书.pdf</p>
        <div class="box">
          <a-button @click="onDownLoad()" type="link" style="padding: 0">下载</a-button>
        </div>
      </a-form-model-item>
      <a-form-model-item label="处置文档上传" prop="dispositionFile">
        <j-upload v-model="form.dispositionFile" :number="1"></j-upload>
        <p>支持扩展名：.rar .zip .doc .docx .pdf .jpg...</p>
        <a-tooltip placement="rightBottom" style="position: absolute;left: 125px;top: 13px;z-index: 1000;"
          :overlayStyle="{ minWidth: `300px`, maxWidth: `400px` }">
          <template slot="title">
            <div>启用模板调取的是对应访谈分类下的访谈模板内容</div>
          </template>
          <a-icon type="question-circle" />
        </a-tooltip>
      </a-form-model-item>
      <a-form-model-item class="sItem" label="通知公安机关">
        <a-switch @change="onSwitchChange" />
      </a-form-model-item>
      <a-form-model-item label="通知单位" v-if="isPolice" prop="noticeUnitId">
        <a-input v-model="form.noticeUnitId" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="通知内容" v-if="isPolice" prop="noticeContent">
        <a-textarea v-model="form.noticeContent" placeholder="请输入" :auto-size="{ minRows: 4, maxRows: 6 }" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import * as uroscopyApi from '@/api/sjsk/check/uroscopy.js'
export default {
  name: 'CancelModal',
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        cancelReason: [{ required: true, message: '请选择' }],
        refuseNum: [{ required: true, message: '请输入' }],
        cancelReasonExplain: [{ required: true, message: '请输入' }],
        dispositionFile: [{ required: true, message: '请选择' }],
        noticeUnitId: [{ required: true, message: '请选择' }],
        noticeContent: [{ required: true, message: '请选择' }],
      },
      isPolice: false,
      loading: false,
      rqUrl: '',
      isOver: false,
      sjskPersonId: ''
    }
  },
  async created() {
  },
  methods: {
    show(record) {
      this.visible = true
      this.isPolice = false
      this.form = {}
      this.form.urineId = record.id
      this.sjskPersonId = record.sjskPersonId
      this.checkUrineCancelNum()       //判断尿检取消次数是否超过三次
    },
    checkUrineCancelNum() {
      uroscopyApi.checkUrineCancelNum({ sjskPersonId: this.sjskPersonId }).then(res => {
        if (res.success) {
          if (res.result) {
            this.isOver = true //取消超过3次
            this.getwfwgxwbgUrl()
          } else {
            this.isOver = false //取消未超过3次
            this.getwgxwgjsUrl()
          }
        }
      })
    },
    //违法-违规行为报告
    getwfwgxwbgUrl() {
      //type类型（1-因阳性违法；2-因取消超过三次违法）
      uroscopyApi.wfwgxwbg({ type: 2, urineId: this.form.urineId }).then(res => {
        if (res.success) {
          if (res.success) {
            this.rqUrl = res.result
          } else {
            this.$message.warning("违法/违规行为报告没正常加载。")
          }
        }
      })
    },
    //违规行为告诫书
    getwgxwgjsUrl() {
      uroscopyApi.wgxwgjs({ sjskPersonId: this.sjskPersonId }).then(res => {
        if (res.success) {
          if (res.success) {
            this.rqUrl = res.result
          } else {
            this.$message.warning("违规行为告诫书没正常加载。")
          }
        }
      })
    },
    handleCancel() {
      this.visible = false
    },
    onDownLoad() {
      window.open(this.rqUrl, "_blank")
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          uroscopyApi.cancelUrine(this.form).then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    },
    onSwitchChange(e) {
      console.log(e);
      if (e) {
        this.isPolice = true
        this.form.isNoticePolice = 1
      } else {
        this.isPolice = false
        this.form.isNoticePolice = 2
      }
    },
  },
}
</script>
<style lang="less" scoped>
.item2 {
  // margin-left: 115px;
  margin-bottom: 20px;

  p {
    display: inline-block;
  }

  .box {
    display: inline-block;
    margin-left: 30px;
    margin-right: 20px;
  }
}
</style>
